package cn.com.callback;
import groovy.json.*;
import groovy.swing.impl.DefaultAction;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import cn.com.elite.EliteBean;
import java.security.MessageDigest;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.wisdge.dataservice.Result;
import java.util.Map;
import java.util.Date;
import java.util.HashMap;
import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONException;
import cn.com.elite.Util;

/**
 * 
 * @Title: apiStateWO
 * @Description:深圳水务系统工单状态数据同步接口--外部接口
 * @author: chieh
 * @date 2019-12-17 02:28:00
 *
 */

public class apiStateWO implements ServiceBackInvoke{
	private static final Log log = LogFactory.getLog(apiStateWO.class);
	public String doCallBack(EliteBean eliteBean,HttpServletRequest request,HttpServletResponse response) {

		response.setCharacterEncoding("UTF-8");

		String getMethod = request.getMethod();
		log.debug("getMethod:" + getMethod);

		String inData = "";
		String outData = "";
		String dataSourceID = "dataSource";

		BufferedReader iny = null;
		if (getMethod.equals("GET")) {
			inData = request.getParameter("data");
			log.debug("原始Get请求参数:" + inData);
		} else {
			InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8");
			iny = new BufferedReader(reader);
			String line;
			while ((line = iny.readLine()) != null) {
				inData += line;
			}
			log.debug("原始Post请求参数:" + inData);
		}

		try {
			// 调用处理信息
			JSONObject inObject = new JSONObject(inData);
			log.debug("处理前的Json：" + inObject.toString());

			String sys_source = GetJV(inObject, "sys_source");
			String sys_flag = GetJV(inObject, "sys_flag");
			String arr_data = GetJV(inObject, "data");

			log.debug("解析信息-sys_source：" + sys_source + " sys_flag：" + sys_flag + " arr_data：" + arr_data);

			if(sys_source=='JT') {//集团
				outData = JTDealWO(eliteBean, sys_flag, arr_data, dataSourceID);
			}
			if(sys_source=='GM') {//光明
				outData = JTDealWO(eliteBean, sys_flag, arr_data, dataSourceID);
			}

		} catch (Exception e) {
			log.error("接口处理异常" + e.getMessage());
			// 失败返回
			outData = "{\"resultcode\":\"0\",\"resultmessage\":\"处理失败(" + e.getMessage() + "),调用问题处理时长(毫秒):2\",\"data\":[]}";

		}

		log.debug("名单接入处理完成返回结果:" + outData);

		return outData;
	}

	// 集团-工单状态处理
	/* *
	 1 派单
	 2 催单
	 3 回退
	 4 待回访
	 5 回访完成
	 6 废弃/失败
	 7 返工
	 8 激活
	 9 延期申请 
	 * */
	public static String JTDealWO(EliteBean eliteBean, String sys_flag, String inJson, String dataSourceID) {
		long startTime = System.currentTimeMillis();
		JSONArray arrJsonArray;
		Map<String, String> dealMap = new HashMap<String, String>();
		Map<String, String> dealMsgMap = new HashMap<String, String>();
		JSONArray resArray = new JSONArray();
		String reJson = null;
		try {
			arrJsonArray = new JSONArray(inJson);

			int arrInt = arrJsonArray.length();
			log.debug("接入List数量:" + arrInt);

			for (int i = 0; i < arrInt; i++) {

				log.debug("当前处理数组位置:" + (i+1));
				JSONObject arrCustJson = arrJsonArray.getJSONObject(i);
				//解析获取当前状态调用信息
				String id = GetJV(arrCustJson, "id");
				String workorderstatus = GetJV(arrCustJson, "workorderstatus");
				String modifyby = GetJV(arrCustJson, "modifyby");
				String modifyname = GetJV(arrCustJson, "modifyname");
				String modifydate = GetJV(arrCustJson, "modifydate");
				String handlingopinions = GetJV(arrCustJson, "handlingopinions");
				String stepnode = GetJV(arrCustJson, "stepnode");
				String timelimit = GetJV(arrCustJson, "timelimit");

				log.debug("解析获取当前状态调用信息-结果id:" + id + " WorkorderStatus:" + workorderstatus + " modifyby:" + modifyby + " modifyname:" + modifyname + " modifydate:" + modifydate + " handlingopinions:" + handlingopinions + " stepnode:" + stepnode + " timelimit:" + timelimit);


				String dealResult = "";
				String dealMessage = "";

				if(isNull(id)||isNull(workorderstatus)||isNull(modifyby)||isNull(modifyname)||isNull(modifydate))
				{
					dealResult = "0";
					dealMessage = isNullMSG("id", id) + isNullMSG("workorderstatus", workorderstatus) + isNullMSG("modifyby", modifyby) + isNullMSG("modifyname", modifyname) + isNullMSG("modifydate", modifydate);
					log.warn("调用参数非空校验异常:" + dealMessage);

				}else {
					//查询现有当前工单编号是否存在现有工单
					def getSelectSQL = eliteBean.chaxun_sql("Select R1.Relation_ID, R3.Handlegroup_ID, R3.Handleby_ID, R3.CurstepStatus, R3.Curstep_Id From Tabcswgdmerge R1 Left Join Objective R2 On R1.Relation_Id = R2.Objective_Guid Left Join Elitetask R3 On R1.Relation_Id = R3.Objective_Guid Where case when R1.uciswordid is not null or R1.uciswordid!='' then R1.uciswordid else R1.workorderid end = '" + id + "'", dataSourceID);
					log.debug("查询当前同步的工单是否已经存在 SQL:" + getSelectSQL + " 结果数量:" + getSelectSQL.size());

					//获取当前工单编号对应的CRM唯一工单Guid Relation_ID
					String old_Relation_ID = null;
					String old_Handlegroup_ID = null;
					String old_Handleby_ID = null;
					String old_CurstepStatus = null;
					String old_Curstep_Id = null;

					if(getSelectSQL.size() > 0) {
						old_Relation_ID = getSelectSQL.get(0).get("Relation_ID");
						old_Handlegroup_ID = getSelectSQL.get(0).get("Handlegroup_ID");
						old_Handleby_ID = getSelectSQL.get(0).get("Handleby_ID");
						old_CurstepStatus = getSelectSQL.get(0).get("CurstepStatus");
						old_Curstep_Id = getSelectSQL.get(0).get("CurstepStatus");
					}else{
						old_Relation_ID = "";
						old_Handlegroup_ID = "";
						old_Handleby_ID = "";
						old_CurstepStatus = "";
						old_Curstep_Id = "";
					}
					log.debug("查询现有当前工单编号是否存在现有工单-结果old_Relation_ID:" + old_Relation_ID + " old_Handlegroup_ID:" + old_Handlegroup_ID + " old_Handleby_ID:" + old_Handleby_ID + " old_CurstepStatus:" + old_CurstepStatus + " old_Curstep_Id:" + old_Curstep_Id);

					// 提前获取工单的上一步处理人
					def getOldSelectSQL = eliteBean.chaxun_sql("Select R1.Handlegroup, R1.Handleby, R1.From_Stepidmap From Workorderpath3 R1 Left Join (Select Workorder_Guid, Max(Modifieddate) Modifieddate From Workorderpath3 Where To_Stepidmap = 'ZUOC2L' And Workorder_Guid = '" + old_Relation_ID + "' Group By Workorder_Guid) R2 On R1.Workorder_Guid = R2.Workorder_Guid And R1.Modifieddate = R2.Modifieddate Where R1.Workorder_Guid = '" + old_Relation_ID + "' And R2.Workorder_Guid Is Not Null", dataSourceID);
					log.debug("查询获取工单的上一步处理人 SQL:" + getOldSelectSQL + " 结果数量:" + getOldSelectSQL.size());

					//获取当前工单编号对应的CRM上一步处理信息
					String before_Handlegroup_ID = null;
					String before_Handleby_ID = null;
					String before_From_Stepidmap = null;

					if(getOldSelectSQL.size() > 0) {
						before_Handlegroup_ID = getOldSelectSQL.get(0).get("Handlegroup");
						before_Handleby_ID = getOldSelectSQL.get(0).get("Handleby");
						before_From_Stepidmap = getOldSelectSQL.get(0).get("From_Stepidmap");
					}else{
						before_Handlegroup_ID = null;
						before_Handleby_ID = null;
						before_From_Stepidmap = null;
					}
					log.debug("提前获取工单的上一步处理人-结果before_Handlegroup_ID:" + before_Handlegroup_ID + " before_Handleby_ID:" + before_Handleby_ID + " before_From_Stepidmap:" + before_From_Stepidmap);

					// 查询下一个工单回访处理人
					def getRevSelectSQL = eliteBean.chaxun_sql("select Top 1 Rec_Guid, Rolegroup_ID, Staff_ID From SZWG_WO_RevisitStaff Where Del_Flag = '0' Order By Revisit_Num", dataSourceID);
					log.debug("查询下一个工单回访处理人 SQL:" + getRevSelectSQL + " 结果数量:" + getRevSelectSQL.size());

					//获取查询下一个工单回访处理人
					String rev_Rec_Guid = null;
					String rev_Handlegroup_ID = null;
					String rev_Handleby_ID = null;

					if(getRevSelectSQL.size() > 0) {
						rev_Rec_Guid = getRevSelectSQL.get(0).get("Rec_Guid");
						rev_Handlegroup_ID = getRevSelectSQL.get(0).get("Rolegroup_ID");
						rev_Handleby_ID = getRevSelectSQL.get(0).get("Staff_ID");
					}else{
						rev_Rec_Guid = "";
						rev_Handlegroup_ID = "";
						rev_Handleby_ID = "";
					}
					log.debug("查询下一个工单回访处理人-结果rev_Rec_Guid:" + rev_Rec_Guid + " rev_Handlegroup_ID:" + rev_Handlegroup_ID + " rev_Handleby_ID:" + rev_Handleby_ID);

					if(old_Relation_ID != null && old_Relation_ID.length() > 0) {
						try {
							//更新工单处理结果
							String[] data=new String[2];
							data[0]  = "Insert Into Tabcswgdmerge_His Select Newid(),* From Tabcswgdmerge Where Relation_Id = '" + old_Relation_ID + "'";
							data[1]  = "Update Tabcswgdmerge Set Workorderstatus = '" + workorderstatus + "', Modifyby = '" + modifyby + "', Modifyname = '" + modifyname + "', Modifydate = '" + modifydate + "', Handlingopinions = '" + handlingopinions + "', Stepnode = '" + stepnode + "', Timelimit = '" + timelimit + "' Where Relation_Id = '" + old_Relation_ID + "'";
							log.debug("更新工单处理结果SQL:" + data.toString());
							int[] dataresult = eliteBean.updateBatch(data, dataSourceID);
							log.debug("更新工单处理结果SQL结果:" + dataresult.toString());

							//判断处理工单状态进行下一步处理
							// 1-派单 UCIS主动调用接口通知当前工单已经派单 CRM处理规则是自动将工单默认下送到处理状态不在需要坐席处理
							if (workorderstatus == "1") {
								String[] data1=new String[4];
								data1[0] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, Oldhandleby_Id, Oldhandlegroup_Id, Handleby_Id, Handlegroup_Id, Workorderstepstatus, Comments, Modifieddate, Workorderstepid, Stepexpired, Child_Id, Stepstart, Cur_Expiredflag, Cur_Expiredlen, From_Stepidmap, To_Stepidmap, Workstarttime, Workendtime) Values ('" + old_Relation_ID + "', NewID(), '"+old_Handleby_ID+"', '"+old_Handlegroup_ID+"', 'SELITE', 'SYSTEM', 'SENDNS', '"+handlingopinions+"', Getdate(), Null, Getdate(), Null, Getdate(), 0, 0, '"+old_CurstepStatus+"', 'ZUOC2L', Getdate(), Getdate())";
								data1[1] = "Insert Into Workorderpath3 (Workorder_Guid, Workorderpath_Guid, Handlegroup, Handleby, Modifieddate, Fromstepid, Tostepid, Woframe_Id, Fromframeid, Handlerole_Id, From_Stepidmap, To_Stepidmap) Values ('" + old_Relation_ID + "', NewID(), 'SYSTEM', 'SELITE', Getdate(), Null, Null, 'ZHS14C', Null, Null, '"+old_CurstepStatus+"', 'ZUOC2L')";
								data1[2] = "Update Objective Set Handlegroup_Id = 'SYSTEM', Handleby_Id = 'SELITE', Objectivestatus = Objectivestatus, Map_Id = Map_Id, Curstep_Id_Map = 'ZUOC2L', Modifiedby = 'SELITE', Modifieddate = Getdate() Where Objective_Guid = '" + old_Relation_ID + "'";
								data1[3] = "Update Elitetask Set Handlegroup_Id = 'SYSTEM', Handleby_Id = 'SELITE', Map_Id = Map_Id, Curstepstatus = Curstepstatus, Curstep_Id = 'ZUOC2L', Curstep_Id_Map = 'ZUOC2L', Objectivesubstatus = Objectivesubstatus, Modifiedby = 'SELITE', Modifieddate = Getdate() Where Objective_Guid = '" + old_Relation_ID + "'";
								log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
								int[] dataresult1 = eliteBean.updateBatch(data1, dataSourceID);
								log.debug("判断处理工单状态进行下一步处理SQL结果:" + dataresult1.toString());

							}

							// 2-催单 CRM处理规则更新工单为催单状态
							if (workorderstatus == "2") {
								String[] data1=new String[1];
								data1[0] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, Handleby_Id, Handlegroup_Id, Workorderstepstatus, Comments, Modifieddate, From_Stepidmap, Workstarttime, Workendtime) Values ('" + old_Relation_ID + "', Newid(), 'SELITE', 'SYSTEM', 'URGE ', '"+handlingopinions+"', Getdate(), '"+old_CurstepStatus+"', Getdate(), Getdate())";
								log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
								int[] dataresult1 = eliteBean.updateBatch(data1, dataSourceID);
								log.debug("判断处理工单状态进行下一步处理SQL结果:" + dataresult1.toString());
							}

							// 3-回退 将工单回退到上一步工单处理人一般是接单坐席
							if (workorderstatus == "3") {
								String[] data1=new String[4];
								data1[0] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, Oldhandleby_Id, Oldhandlegroup_Id, Handleby_Id, Handlegroup_Id, Workorderstepstatus, Comments, Modifieddate, Workorderstepid, Stepexpired, Child_Id, Stepstart, Cur_Expiredflag, Cur_Expiredlen, From_Stepidmap, To_Stepidmap, Workstarttime, Workendtime) Values ('" + old_Relation_ID + "', NewID(), '"+old_Handleby_ID+"', '"+old_Handlegroup_ID+"', '" + before_Handleby_ID + "', '" + before_Handlegroup_ID + "', 'BACK', '"+handlingopinions+"', Getdate(), Null, Getdate(), Null, Getdate(), 0, 0, '"+old_CurstepStatus+"', '" + before_From_Stepidmap + "', Getdate(), Getdate())";
								data1[1] = "Delete From Workorderpath3 Where Workorder_Guid = '" + old_Relation_ID + "' And From_Stepidmap = '" + before_From_Stepidmap + "' And To_stepidmap = '" + old_Curstep_Id + "'";
								data1[2] = "Update Objective Set Handlegroup_Id = '" + before_Handlegroup_ID + "', Handleby_Id = '" + before_Handleby_ID + "', Objectivestatus = Objectivestatus, Map_Id = Map_Id, Curstep_Id_Map = '" + before_From_Stepidmap + "', Modifiedby = 'SELITE', Modifieddate = Getdate() Where Objective_Guid = '" + old_Relation_ID + "'";
								data1[3] = "Update Elitetask Set Handlegroup_Id = '" + before_Handlegroup_ID + "', Handleby_Id = '" + before_Handleby_ID + "', Map_Id = Map_Id, Curstepstatus = 'BACK', Curstep_Id = '" + before_From_Stepidmap + "', Curstep_Id_Map = '" + before_From_Stepidmap + "', Objectivesubstatus = Objectivesubstatus, Modifiedby = 'SELITE', Modifieddate = Getdate() Where Objective_Guid = '" + old_Relation_ID + "'";
								log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
								int[] dataresult1 = eliteBean.updateBatch(data1, dataSourceID);
								log.debug("判断处理工单状态进行下一步处理SQL结果:" + dataresult1.toString());
							}

							// 4-待回访 将工单下送到回访节点
							if (workorderstatus == "4") {
								String[] data1=new String[4];
								data1[0] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, Oldhandleby_Id, Oldhandlegroup_Id, Handleby_Id, Handlegroup_Id, Workorderstepstatus, Comments, Modifieddate, Workorderstepid, Stepexpired, Child_Id, Stepstart, Cur_Expiredflag, Cur_Expiredlen, From_Stepidmap, To_Stepidmap, Workstarttime, Workendtime) Values ('" + old_Relation_ID + "', NewID(), '"+old_Handleby_ID+"', '"+old_Handlegroup_ID+"', '" + rev_Handleby_ID + "', '" + rev_Handlegroup_ID + "', 'SENDNS', '"+handlingopinions+"', Getdate(), Null, Getdate(), Null, Getdate(), 0, 0, '"+old_CurstepStatus+"', '0DKTUC', Getdate(), Getdate())";
								data1[1] = "Insert Into Workorderpath3 (Workorder_Guid, Workorderpath_Guid, Handlegroup, Handleby, Modifieddate, Fromstepid, Tostepid, Woframe_Id, Fromframeid, Handlerole_Id, From_Stepidmap, To_Stepidmap) Values ('" + old_Relation_ID + "', NewID(), 'SYSTEM', 'SELITE', Getdate(), Null, Null, 'ZHS14C', Null, Null, '"+old_CurstepStatus+"', '0DKTUC')";
								data1[2] = "Update Objective Set Handlegroup_Id = '" + rev_Handlegroup_ID + "', Handleby_Id = '" + rev_Handleby_ID + "', Objectivestatus = Objectivestatus, Map_Id = Map_Id, Curstep_Id_Map = '0DKTUC', Modifiedby = 'SELITE', Modifieddate = Getdate() Where Objective_Guid = '" + old_Relation_ID + "'";
								data1[3] = "Update Elitetask Set Handlegroup_Id = '" + rev_Handlegroup_ID + "', Handleby_Id = '" + rev_Handleby_ID + "', Map_Id = Map_Id, Curstepstatus = Curstepstatus, Curstep_Id = '0DKTUC', Curstep_Id_Map = '0DKTUC', Objectivesubstatus = Objectivesubstatus, Modifiedby = 'SELITE', Modifieddate = Getdate() Where Objective_Guid = '" + old_Relation_ID + "'";
								data1[3] = "Insert Into Szwg_Wo_Revisitstaff_List (List_Guid, Rec_Guid, Workorder_Id, Rolegroup_Id, Staff_Id, Createdtime) Values (NewID(), '" + rev_Rec_Guid + "', '" + id + "', '" + rev_Handlegroup_ID + "', '" + rev_Handleby_ID + "', GetDate())";
								data1[3] = "Update SZWG_WO_RevisitStaff Set Revisit_Num = IsNull(Revisit_Num, 0) + 1 Where Rec_Guid = '" + rev_Rec_Guid + "'";
								log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
								int[] dataresult1 = eliteBean.updateBatch(data1, dataSourceID);
								log.debug("判断处理工单状态进行下一步处理SQL结果:" + dataresult1.toString());
							}

							// 5-回访完成 将工单推送到结案状态，并更新回访结果--暂定通过回访接口送回访结果
							if (workorderstatus == "5") {
								String[] data1=new String[4];
								data1[0] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, Oldhandleby_Id, Oldhandlegroup_Id, Handleby_Id, Handlegroup_Id, Workorderstepstatus, Comments, Modifieddate, Workorderstepid, Stepexpired, Child_Id, Stepstart, Cur_Expiredflag, Cur_Expiredlen, From_Stepidmap, To_Stepidmap, Workstarttime, Workendtime) Values ('" + old_Relation_ID + "', NewID(), '"+old_Handleby_ID+"', '"+old_Handlegroup_ID+"', 'SELITE', 'SYSTEM', 'END', '"+handlingopinions+"', Getdate(), Null, Getdate(), Null, Getdate(), 0, 0, '"+old_CurstepStatus+"', '0DKTUC', Getdate(), Getdate())";
								data1[1] = "Insert Into Workorderpath3 (Workorder_Guid, Workorderpath_Guid, Handlegroup, Handleby, Modifieddate, Fromstepid, Tostepid, Woframe_Id, Fromframeid, Handlerole_Id, From_Stepidmap, To_Stepidmap) Values ('" + old_Relation_ID + "', NewID(), 'SYSTEM', 'SELITE', Getdate(), Null, Null, 'ZHS14C', Null, Null, '"+old_CurstepStatus+"', '0DKTUC')";
								data1[2] = "Update Objective Set Handlegroup_Id = 'SYSTEM', Handleby_Id = 'SELITE', Objectivestatus = 'END', Map_Id = Map_Id, Curstep_Id_Map = '0DKTUC', Modifiedby = 'SELITE', Modifieddate = Getdate() Where Objective_Guid = '" + old_Relation_ID + "'";
								data1[3] = "Update Elitetask Set Handlegroup_Id = 'SYSTEM', Handleby_Id = 'SELITE', Map_Id = Map_Id, Curstepstatus = 'END', Curstep_Id = '0DKTUC', Curstep_Id_Map = '0DKTUC', Objectivesubstatus = Objectivesubstatus, Modifiedby = 'SELITE', Modifieddate = Getdate() Where Objective_Guid = '" + old_Relation_ID + "'";
								log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
								int[] dataresult1 = eliteBean.updateBatch(data1, dataSourceID);
								log.debug("判断处理工单状态进行下一步处理SQL结果:" + dataresult1.toString());
							}

							// 6-废弃/失败 直接将工单失败结案
							if (workorderstatus == "6") {
								String[] data1=new String[4];
								data1[0] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, Oldhandleby_Id, Oldhandlegroup_Id, Handleby_Id, Handlegroup_Id, Workorderstepstatus, Comments, Modifieddate, Workorderstepid, Stepexpired, Child_Id, Stepstart, Cur_Expiredflag, Cur_Expiredlen, From_Stepidmap, To_Stepidmap, Workstarttime, Workendtime) Values ('" + old_Relation_ID + "', NewID(), '"+old_Handleby_ID+"', '"+old_Handlegroup_ID+"', 'SELITE', 'SYSTEM', 'FAILED', '"+handlingopinions+"', Getdate(), Null, Getdate(), Null, Getdate(), 0, 0, '"+old_CurstepStatus+"', '0DKTUC', Getdate(), Getdate())";
								data1[1] = "Insert Into Workorderpath3 (Workorder_Guid, Workorderpath_Guid, Handlegroup, Handleby, Modifieddate, Fromstepid, Tostepid, Woframe_Id, Fromframeid, Handlerole_Id, From_Stepidmap, To_Stepidmap) Values ('" + old_Relation_ID + "', NewID(), 'SYSTEM', 'SELITE', Getdate(), Null, Null, 'ZHS14C', Null, Null, '"+old_CurstepStatus+"', '0DKTUC')";
								data1[2] = "Update Objective Set Handlegroup_Id = 'SYSTEM', Handleby_Id = 'SELITE', Objectivestatus = 'FAILED', Map_Id = Map_Id, Curstep_Id_Map = '0DKTUC', Modifiedby = 'SELITE', Modifieddate = Getdate() Where Objective_Guid = '" + old_Relation_ID + "'";
								data1[3] = "Update Elitetask Set Handlegroup_Id = 'SYSTEM', Handleby_Id = 'SELITE', Map_Id = Map_Id, Curstepstatus = 'FAILED', Curstep_Id = '0DKTUC', Curstep_Id_Map = '0DKTUC', Objectivesubstatus = Objectivesubstatus, Modifiedby = 'SELITE', Modifieddate = Getdate() Where Objective_Guid = '" + old_Relation_ID + "'";
								log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
								int[] dataresult1 = eliteBean.updateBatch(data1, dataSourceID);
								log.debug("判断处理工单状态进行下一步处理SQL结果:" + dataresult1.toString());
							}

							// 7-返工 将工单回退到接单坐席,不确定返工和CRM什么关系，暂时按退回处理
							if (workorderstatus == "7") {
								String[] data1=new String[4];
								data1[0] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, Oldhandleby_Id, Oldhandlegroup_Id, Handleby_Id, Handlegroup_Id, Workorderstepstatus, Comments, Modifieddate, Workorderstepid, Stepexpired, Child_Id, Stepstart, Cur_Expiredflag, Cur_Expiredlen, From_Stepidmap, To_Stepidmap, Workstarttime, Workendtime) Values ('" + old_Relation_ID + "', NewID(), '"+old_Handleby_ID+"', '"+old_Handlegroup_ID+"', '" + before_Handleby_ID + "', '" + before_Handlegroup_ID + "', 'BACK', '"+handlingopinions+"', Getdate(), Null, Getdate(), Null, Getdate(), 0, 0, '"+old_CurstepStatus+"', '" + before_From_Stepidmap + "', Getdate(), Getdate())";
								data1[1] = "Delete From Workorderpath3 Where Workorder_Guid = '" + old_Relation_ID + "' And From_Stepidmap = '" + before_From_Stepidmap + "' And To_stepidmap = '" + old_Curstep_Id + "'";
								data1[2] = "Update Objective Set Handlegroup_Id = '" + before_Handlegroup_ID + "', Handleby_Id = '" + before_Handleby_ID + "', Objectivestatus = Objectivestatus, Map_Id = Map_Id, Curstep_Id_Map = '" + before_From_Stepidmap + "', Modifiedby = 'SELITE', Modifieddate = Getdate() Where Objective_Guid = '" + old_Relation_ID + "'";
								data1[3] = "Update Elitetask Set Handlegroup_Id = '" + before_Handlegroup_ID + "', Handleby_Id = '" + before_Handleby_ID + "', Map_Id = Map_Id, Curstepstatus = 'BACK', Curstep_Id = '" + before_From_Stepidmap + "', Curstep_Id_Map = '" + before_From_Stepidmap + "', Objectivesubstatus = Objectivesubstatus, Modifiedby = 'SELITE', Modifieddate = Getdate() Where Objective_Guid = '" + old_Relation_ID + "'";
								log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
								int[] dataresult1 = eliteBean.updateBatch(data1, dataSourceID);
								log.debug("判断处理工单状态进行下一步处理SQL结果:" + dataresult1.toString());
							}

							// 8-激活 基于UCIS调用时指定的激活节点将工单重新打开
							// 9-延期申请 将工单状态变更为延期申请，更新延期申请时间
							if (workorderstatus == "9") {
								String[] data1=new String[1];
								data1[0] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, Oldhandleby_Id, Oldhandlegroup_Id, Handleby_Id, Handlegroup_Id, Workorderstepstatus, Comments, Modifieddate, Workorderstepid, Stepexpired, Child_Id, Stepstart, Cur_Expiredflag, Cur_Expiredlen, From_Stepidmap, To_Stepidmap, Workstarttime, Workendtime) Values ('" + old_Relation_ID + "', NewID(), '"+old_Handleby_ID+"', '"+old_Handlegroup_ID+"', 'SELITE', 'SYSTEM', 'OPEN', '"+handlingopinions+"', Getdate(), Null, Getdate(), Null, Getdate(), 0, 0, '"+old_CurstepStatus+"', '" + old_CurstepStatus + "', Getdate(), Getdate())";
								log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
								int[] dataresult1 = eliteBean.updateBatch(data1, dataSourceID);
								log.debug("判断处理工单状态进行下一步处理SQL结果:" + dataresult1.toString());
							}

							dealResult = "1";
						} catch (Exception e) {
							dealResult = "0";
							dealMessage = "当前处理状态变更的工单整体处理SQL异常";
							log.error(dealMessage + ":" + e.getMessage());
						}

					}else {
						dealResult = "0";
						dealMessage = "当前处理结果变更的工单在CRM系统不存在ID";
						log.warn(dealMessage + ":" + id);
					}
				}


				dealMap.put(id, dealResult);
				dealMsgMap.put(id, dealMessage);
			}

			// 将每条处理结果转换为JSONArray
			int j = 0;
			for (Map.Entry<String, String> m : dealMap.entrySet()) {
				JSONObject resObject = new JSONObject();
				resObject.put("id", m.getKey());
				resObject.put("code", m.getValue());
				resObject.put("message", dealMsgMap.get(m.getKey()).toString());
				resArray.put(j, resObject);
				j++;
			}


			long endTime = System.currentTimeMillis();
			// 包装拼接返回JSONObject
			reJson = "{\"resultcode\":\"1\",\"resultmessage\":\"处理完成、调用问题处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":" + resArray.toString() + "}";

		} catch (JSONException e) {
			long endTime = System.currentTimeMillis();
			reJson = "{\"resultcode\":\"0\",\"resultmessage\":\"处理失败(" + e.getMessage() + "),调用问题处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":[]}";
		}

		return reJson;
	}


	// 光明-工单状态处理
	public static String GMDealWO(EliteBean eliteBean, String sys_flag, String inJson, String dataSourceID) {

		String reJson = null;
		long startTime = System.currentTimeMillis();
		long endTime = System.currentTimeMillis();

		// 包装拼接返回JSONObject
		reJson = "{\"resultcode\":\"1\",\"resultmessage\":\"处理完成、调用问题处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":}";

		return reJson;
	}

	// 解析Json值
	public static String GetJV(JSONObject inJson, String getKey) {
		String Rev_Value = null;
		try {
			Rev_Value = inJson.getString(getKey);
		} catch (Exception e) {
			try {
				Rev_Value = String.valueOf(inJson.getInt(getKey));
			} catch (Exception e1) {
				try {
					Rev_Value = String.valueOf(inJson.getJSONArray(getKey));
				} catch (Exception e2) {
					try {
						Rev_Value = String.valueOf(inJson.getJSONObject(getKey));
					} catch (Exception e3) {
						Rev_Value = "";
					}
				}
			}
		} finally {
			if (Rev_Value == null || Rev_Value.equals("null") || Rev_Value.length() <= 0) {
				Rev_Value = "";
			} else {
			}
		}
		log.debug("解析-k=" + getKey + " v=" + Rev_Value);
		return Rev_Value;
	}

	// 非空校验isNull
	public static boolean isNull(String value) {

		boolean reFlag = true;
		if (value == "" || value.length() <= 0) {
			reFlag = true;
		} else {
			reFlag = false;
		}

		return reFlag;
	}

	// 非空校验isNullMSG
	public static String isNullMSG(String key, String value) {

		String reResult = "";
		if (value == "" || value.length() <= 0) {
			reResult = key + "必填字段;";
		}
		return reResult;
	}

}
